package tema13.ejercicioRotar10;

import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EJMiPanel extends JPanel {
	private int a, b, aIncremento, bIncremento, alto, ancho;
	private final int c = 100;
	private final int d = 100;
	private Random r = new Random();

	public void paint(Graphics g) {
		super.paint(g);
		g.drawOval(a, b, c, d);
	}

	public void crear() {
		this.ancho = Ej10Rotar.ancho;
		this.alto = Ej10Rotar.alto;
		a = r.nextInt(100);
		b = r.nextInt(100);
		if (r.nextInt(2) == 0) {
			aIncremento = 1;
		} else {
			aIncremento = -1;
		}
		if (r.nextInt(2) == 0) {
			bIncremento = 1;
		} else {
			bIncremento = -1;
		}
	}

	private void esperar() {
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 1000; j++) {
				setVisible(true);
			}
		}
	}

	public void comprobar() {
		int tmp = (this.ancho - c);
		if (a >= tmp) {
			aIncremento = -1;
			a = tmp;
		}
		if (a <= 0) {
			aIncremento = +1;
			a = 0;
		}

		tmp = (this.alto - d);

		if (b >= tmp) {
			bIncremento = -1;
			b = tmp;
		}
		if (b < 0) {
			bIncremento = +1;
			b = 0;
		}

	}

	public void rotar() {
		while (true) {
			esperar();
			a += aIncremento;
			b += bIncremento;
			comprobar();
			this.repaint();
			this.alto = this.getHeight();
			this.ancho = this.getWidth();
		}
	}

}
